package insert

import (
	"context"
	"strings"

	"server_1.0/api/internal/svc"
	"server_1.0/api/internal/types"
	"server_1.0/service/college/college"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertCollegeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewInsertCollegeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertCollegeLogic {
	return &InsertCollegeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InsertCollegeLogic) InsertCollege(req *types.ColInsertReq) (resp *types.PostReps, err error) {
	code := util.Success
	Msg := "增添成功"
	if strings.Index(req.Cname, "学院") == -1 {
		code = util.ParamError
		Msg = "增添失败"
		return &types.PostReps{
			Code: code,
			Msg:  Msg,
			Data: map[string]interface{}{
				"res": nil,
			},
		}, nil
	}
	ins, _ := l.svcCtx.CollegeRpc.Insert(l.ctx, &college.InsertReq{
		No:   req.Cno,
		Name: req.Cname,
	})
	if ins.Count == 0 {
		code = util.AddError
		Msg = "增添失败"
	}
	return &types.PostReps{
		Code: code,
		Msg:  Msg,
		Data: map[string]interface{}{
			"res": ins.QRs,
		},
	}, nil
}
